package com.kylin.module.admin.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.CatDict;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.admin.business.ManageDictBlogic;
import com.kylin.module.admin.form.DictForm;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

/**
 * 后台管理接口：管理系统的属性字典数据
 * @author lzh
 *
 */
public class ManageDictAction extends BaseDispatchAction implements BaseActionInterface {

	protected static Logger log = Logger.getLogger(ManageDictAction.class);

	public ActionForward add(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		DictForm form = (DictForm) actionForm;
		try {
			ManageDictBlogic blogic = new ManageDictBlogic();
			System.out.println("\n " + form);
			blogic.addDict(form);
		}catch (Exception e) {
			doException( request, e, "addBusinessException");
		}
		return search( mapping,  actionForm, request,  response);
	}

	public ActionForward bfUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查看修改选项 : "+request.getParameter("name"));
		DictForm form = (DictForm)actionForm;
		String forward = "bfUpdate";
		String id = request.getParameter("id");
		try{
			ManageDictBlogic bean = new ManageDictBlogic();
			Dict dict = bean.getDictById(new Long(id));
			form.setId(dict.getId());
			form.setName(dict.getName());
			form.setDescription(dict.getDescription());
			if(dict.getParent()!=  null){
				form.setParent(dict.getParent().getId());
			}
			form.setLanguage(dict.getLanguage());
			form.setOrdering(dict.getOrdering());
		}catch (Exception e) {
			forward = doException( request, e, "bfUpdateBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		log.debug("删除字典内容列表: "+request.getParameter("catId"));
		String forward = "list";
		String id = request.getParameter("id");
		try{
			ManageDictBlogic bean = new ManageDictBlogic();
			bean.deleteDictById(new Long(id));
			return search( mapping, actionForm, request, response);
		}catch (Exception e) {
			forward = doException( request, e, "list");
			return mapping.findForward(forward);
		}
	}

	public ActionForward search(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
log.debug("查询字典内容列表: "+request.getParameter("catId"));
		String forward = "list";
		int pageNum = 1;
		String searchId = request.getParameter("searchId");
		CriteriaInfo cinfo = null;
		try {
			Long catId  = new Long(request.getParameter("catId"));
			Long parentId  = null;
			try{ 
				parentId = new Long(request.getParameter("parent")); 
			}catch(Exception e){
				parentId = new Long(0);
			}
			CatDict cat = (CatDict) BaseHibernateDAO.findById(CatDict.class, catId);
			Dict parent = (Dict) BaseHibernateDAO.findById(Dict.class, parentId);
			if(parentId.equals(new Long(0)) ){
				parent = null;
			}
			request.setAttribute("CatDict", cat);
			request.setAttribute("parent", parent);

			if (request.getParameter("page") != null) {
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if (searchId == null) {
				PropertyCondition[] pConditions = new PropertyCondition[] {
						new PropertyCondition("catdict", cat,PropertyCondition.OPERATOR_EQ ),
						new PropertyCondition("parent", parent,PropertyCondition.OPERATOR_EQ )
				};
				
				cinfo = new CriteriaInfo(Dict.class, pConditions, Order.asc("ordering"));
				searchId = this.addUserCacheCriteriaInfo(request, cinfo);
				request.setAttribute(Constants.KEY_REQUEST_QUERYID, searchId);
			}
			cinfo = getUserCacheCriteriaInfo(request, searchId);
			ManageDictBlogic bean = new ManageDictBlogic();
			DisplayPage page = bean.getDictList(pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
			
		}catch (Exception e) {
			forward = doException( request, e, "searchBusinessException");
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		log.info("in dict update");
		
		DictForm form = (DictForm) actionForm;
		if (form.getId().longValue() != 0){
			log.info(form.getId());
			try {
				ManageDictBlogic blogic = new ManageDictBlogic();
				System.out.println("\n " + form);
				blogic.updateDict(form);
				request.setAttribute(Constants.MSGKEY, new MsgKey("","修改成功"));
			}catch (Exception e) {
				return mapping.findForward( doException( request, e, "addBusinessException") );
			}			
		}
		return search( mapping,  actionForm, request, response);
	}

}
